/*
 * CTDR_Strip.cpp
 *
 *  Created on: 22 d�c. 2010
 *      Author: Mathieu
 */

#include "CTDR_Strip.h"
#include <math.h>
#include <iostream>
#include <fstream>
#include "TFile.h"
#include "TH1F.h"

using namespace std;

double highest(double a, double b){
	if(a>b) return a;
	else return b;
}


CTDR_Strip::CTDR_Strip(double LX,double LY, double LZ,TString file):CTDR_Electrode(LX, LY, LZ)  {


	cout << TString::Format("[CTDR_Strip] Opening Ramo Potential histogram file %s",file.Data()) << endl;
	TFile *f =TFile::Open(file);

	//gEx= (TGraph2D*)f->FindObject("gEx");
	//gEy= (TGraph2D*)f->FindObject("gEy");

	cout << TString::Format("[CTDR_Strip] Reading Ramo Potential Histogram") << endl;
	f->GetObject("hEx",Ramo);
	Ramo->Print();

	V_Ramo=0;
	Le=0;
	Lh=0;
	Ve0=0;
	Vh0=0;
}



void CTDR_Strip::SetV(double x, double y, double z){

	if(x<LX && x>0 && y<LY && y>0 && z>0 && z<zmax)V_Ramo=Ramo->Interpolate(x,z);
	else if(x<LX && x>0 && y<LY && y>0 && z<=0 )V_Ramo=Ramo->Interpolate(x,0.01);
	else if(x<LX && x>0 && y<LY && y>0 && z>=zmax )V_Ramo=Ramo->Interpolate(x,zmax-0.01);

//	if(x<LX && x>0 && y<LY && y>0 && z>0 && z<zmax)V_Ramo=(zmax-z)/zmax;
//	else if(x<LX && x>0 && y<LY && y>0 && z<=0 )V_Ramo=1;
//	else if(x<LX && x>0 && y<LY && y>0 && z>=zmax )V_Ramo=0;

	else V_Ramo=0;
	//V_Ramo=Ramo->Interpolate(x,z);
}

void CTDR_Strip::AddData(CTDR_Interaction e, CTDR_Interaction h){

	double Ne=e.GetN();
	double Nh=h.GetN();
	double Ve=0;
	double Vh=0;

	//cout << "Ne=" << Ne << "Nh=" << Nh << endl;
	for(int i=0;i<Ne;i++){
		SetV(e.GetCharge(i).GetX(),e.GetCharge(i).GetY(),e.GetCharge(i).GetZ());
		Ve+=-1*V_Ramo;
	};

	for(int i=0;i<Nh;i++){
		SetV(h.GetCharge(i).GetX(),h.GetCharge(i).GetY(),h.GetCharge(i).GetZ());
		Vh+=V_Ramo;
	};

	t_e.push_back(e.Gett());
	V_e.push_back(Ve);
	t_h.push_back(h.Gett());
	V_h.push_back(Vh);

    //cout << Vh << endl;

	Le++;
	Lh++;


};

void CTDR_Strip::AddDataHoles(CTDR_Interaction e, CTDR_Interaction h){

	double Nh=h.GetN();
	double Vh=0;


	for(int i=0;i<Nh;i++){
		SetV(h.GetCharge(i).GetX(),h.GetCharge(i).GetY(),h.GetCharge(i).GetZ());
		Vh+=V_Ramo;
	};


	t_h.push_back(h.Gett());
	V_h.push_back(Vh);
	Lh++;


};

void CTDR_Strip::AddDataElectrons(CTDR_Interaction e, CTDR_Interaction h){

	double Ne=e.GetN();
	double Ve=0;


	for(int i=0;i<Ne;i++){
		SetV(e.GetCharge(i).GetX(),e.GetCharge(i).GetY(),e.GetCharge(i).GetZ());
		Ve+=-1*V_Ramo;
	};


	t_e.push_back(e.Gett());
	V_e.push_back(Ve);
	Le++;


};







void CTDR_Strip::SetV0(CTDR_Interaction e, CTDR_Interaction h){

	double Ne=e.GetN();
	double Nh=h.GetN();
	double Ve=0;
	double Vh=0;
	for(int i=0;i<Ne;i++){
		SetV(e.GetCharge(i).GetX(),e.GetCharge(i).GetY(),e.GetCharge(i).GetZ());
		Ve+=-1*V_Ramo;
	};

	for(int i=0;i<Nh;i++){
		SetV(h.GetCharge(i).GetX(),h.GetCharge(i).GetY(),h.GetCharge(i).GetZ());
		Vh+=V_Ramo;
	};

	Vh0=Vh;
	Ve0=Ve;





};


void CTDR_Strip::WriteData(int event,CTDR_Interaction e, CTDR_Interaction h){

	pulse_e = new TGraph(Le);
	pulse_h = new TGraph(Lh);

	for(int i =0; i<Le;i++)
	{
		pulse_e->SetPoint(i,t_e[i],V_e[i]);
	};
	for(int i =0; i<Lh;i++)
	{
		pulse_h->SetPoint(i,t_h[i],V_h[i]);
	}



	pulse_e->SetName("electron pulse");
	pulse_h->SetName("hole pulse");
	TFile *f = new TFile(TString::Format("event_%d.root",event),"recreate");
	pulse_e->Write();
	pulse_h->Write();

	TH1F *he=pulse_e->GetHistogram();
	TH1F *hh=pulse_h->GetHistogram();
	double xmax_e=e.Gett();
	double xmax_h=h.Gett();

	TH1F *pulse_total= new TH1F("pulse total","",1000,0,highest(xmax_e,xmax_h));


	double vtotal=0;
	double t=0;
	double dt=0;
	if (xmax_e>xmax_h){

		vtotal=0;
		dt=xmax_e/999;
		for(int i=0;i<1000;i++){
			t=i*dt;
			vtotal=0;
			vtotal+=pulse_e->Eval(t);
			if(t<xmax_h)vtotal+=pulse_h->Eval(t);
			pulse_total->SetBinContent(i,vtotal);
		}


	}

	else {
		vtotal=0;
		int bin=0;
		dt=xmax_h/999;
		for(int i=0;i<1000;i++){
			t=i*dt;
			vtotal=0;
			vtotal+=pulse_h->Eval(t);
			if(t<xmax_e)vtotal+= pulse_e->Eval(t);
			//cout << t << pulse_h->Eval(t) << " " << pulse_e->Eval(t) <<  endl;
			pulse_total->SetBinContent(i,vtotal);
			bin++;
		}

	}

	pulse_total->Write();

	Qtot= pulse_total->GetMinimum();

	f->Close();

	SetV(120,120,0);
	//cout << xmax_e <<endl;


	delete f;
	delete hh;
	delete he;
	//delete pulse_total;

};

void CTDR_Strip::Reset(){

//	t_e.erase(t_e.begin(),t_e.end()+1);
//	V_e.erase(V_e.begin(),V_e.end()+1);
//	t_h.erase(t_h.begin(),t_h.end()+1);
//	V_h.erase(V_h.begin(),V_h.end()+1);
	t_e.clear();
	V_e.clear();
	t_h.clear();
	V_h.clear();
	Le=0;
	Lh=0;

//delete pulse_e;
//	delete pulse_h;




};


int CTDR_Strip::GetLength(TString file){

	ifstream in(file);
	int n=0;
	double a,b,c;
	while(!in.eof())
	{	in >> a >> b >> c ;
		n++;
	};
	in.close();
	return n;

}




